package com.lysh.http.client;

import com.lysh.http.api.API;
import com.lysh.http.api.ApiParams;
import com.lysh.http.exception.SDKException;
import com.lysh.http.handler.DefaultHttpClient;
import com.lysh.http.handler.Executor;
import org.apache.http.impl.client.CloseableHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liyishan
 * @date 2023/4/19 12:52
 */
public class DefaultLetsignClient extends LetsignClient {
    private final CloseableHttpClient httpClient = new DefaultHttpClient();

    private static final Logger log = LoggerFactory.getLogger(DefaultLetsignClient.class);

    protected String invoke(API api, ApiParams apiParams, String docs) throws SDKException {
        api.setUrl(super.getUrl() + api.getPath());
        api.setAPIParams(apiParams);
        long startTime = System.currentTimeMillis();
        String result = this.getResponseBody(api);
        log.info(docs + "，执行结果，{}，耗时：{}ms", result, System.currentTimeMillis() - startTime);
        return result;
    }

    private String getResponseBody(API api) throws SDKException {
        api.getAPIParams().setCommonParams(super.getAppCode(), api.getPath(), super.getVersion(), super.getAppSecret());
        Executor executor = new Executor();
        return executor.execute(this.httpClient, api);
    }

}
